﻿using gfLibrary;
using IYAKASAPortal.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Text;


namespace IYAKASAPortal.LabourVerify
{
    public partial class LabourVerify : System.Web.UI.Page
    {
        private UserInfo userInfo { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Session["UserName"] != null && Session["UserName"].ToString() != "")
            //{
            //    string userName = Session["UserName"].ToString();
            //    userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);
            //}
            //else
            //{
            //    gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
            //}

            if (!IsPostBack)
            {
                GetTestItemsValues(chkListType);
                
                binddrplist();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string strKey = txtKey.Text.Trim();
            if (string.IsNullOrEmpty(strKey))
            {
                gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "请输入号牌号码或VIN!");
                txtKey.Focus();
                return;
            }

            lstInfo.Items.Clear();

            LOGIN_VEHICLE_INFO_BLL bll = new LOGIN_VEHICLE_INFO_BLL();
            var loginInfo = bll.GetModelList("HPHM like '%" + strKey + "%' or VIN like '%" + strKey + "%'", "ID desc", 1, 50);
    
            ListItem item;
            for (int i = 0; i < loginInfo.Length; i++)
            {
                string jclsh = Getjclsh(loginInfo[i].HPHM, loginInfo[i].HPZLDH);

                if (jclsh!="")
                {
                    string str = jclsh.Substring(0, 1);
                    if (str == "A" || str == "Z")
                    {
                 item = new ListItem();
                 item.Text = loginInfo[i].HPHM + "[" + loginInfo[i].VIN + "]";
                 item.Value = loginInfo[i].ID.ToString();
                 lstInfo.Items.Add(item);

                    }


                }
             

            }

            //ListItem item;
            //foreach (var v in loginInfo)
            //{
            //    item = new ListItem();
            //    item.Text = v.HPHM + "[" + v.VIN + "]";
            //    item.Value = v.ID.ToString();
            //    lstInfo.Items.Add(item);
            //}
        }

        protected void btnRight_Click(object sender, EventArgs e)
        {
            string strID = lstInfo.SelectedValue;

            if (string.IsNullOrEmpty(strID))
            {
                gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "请选择一条记录!");
            }
            //绑定车牌号码和VIN
            LOGIN_VEHICLE_INFO_BLL bll = new LOGIN_VEHICLE_INFO_BLL();
            var infobll = bll.GetModelList("ID='" + strID + "'");

            for (int i = 0; i < infobll.Length; i++)
            {
                txtHPHM.Text = infobll[i].HPHM;
                txtVIN.Text = infobll[i].VIN;
                string jclshdt = Getjclsh(infobll[i].HPHM, infobll[i].HPZLDH);
                jclsh.Value = jclshdt;

                
            }
            BindTree(chkListType, tree);
            ButtonOK.Visible = true;
        }

        private void BindTree(CheckBoxList chk, TreeView treeView)
        {
            List<string> lst = new List<string>();
            for (int i = 0; i < chk.Items.Count; i++)
            {
                if (chk.Items[i].Selected)
                {
                    lst.Add(chk.Items[i].Value);
                }
            }

            string strSql = "select MITEMID,MITEMNAME from OUTLOOK_CHASISS_SUBITEM where APPLYCLASS=0 ";
            if (lst.Count == 0)
            {
                treeView.Visible = false;
                return;
            }
            else if (lst.Count == 1)
            {
                if (lst[0] == "1")
                {
                    strSql += " and MITEMID <6 group by  MITEMID,MITEMNAME order by MITEMID asc";
                }
                else if (lst[0] == "2")
                {
                    strSql += " and MITEMID <8 and MITEMID >5  group by  MITEMID,MITEMNAME order by MITEMID asc";
                }
            }
            else
            {
                strSql += " and MITEMID <8 group by  MITEMID,MITEMNAME  order by MITEMID asc";
            }

            treeView.Visible = true;
            treeView.Nodes.Clear();

            TreeNode root = new TreeNode();
            root.ShowCheckBox = false;
            root.Text = "车辆人工检验";
            root.Value = "-1";

            DataTable dt = new DataTable();
            DBUtility.DbHelper.GetTable(strSql, ref dt);

            TreeNode node;
            TreeNode nodeEx;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                node = new TreeNode();
                nodeEx = new TreeNode();
                node.Text = dt.Rows[i]["MITEMID"].ToString() + " "+ dt.Rows[i]["MITEMNAME"].ToString();
                node.Value = dt.Rows[i]["MITEMID"].ToString();
                node.ShowCheckBox = false;
                node.ChildNodes.Add(nodeEx);
                root.ChildNodes.Add(node);
            }


            treeView.Nodes.Add(root);
        }

        protected void tree_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            string strID = e.Node.Value;
            if (e.Node.ChildNodes.Count > 0 && string.IsNullOrEmpty(e.Node.ChildNodes[0].Value))
            {
                e.Node.ChildNodes.Clear();
            }

            string strSql = "select MID, MNAME from OUTLOOK_CHASISS_SUBITEM where MITEMID=" + strID + " and APPLYCLASS=0  group by MID, MNAME order by MID asc";

            DataTable dt = new DataTable();
            DBUtility.DbHelper.GetTable(strSql, ref dt);

            if (dt.Rows.Count > 0 && e.Node.ChildNodes.Count > 0)
            {
                return;
            }

            TreeNode node;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                node = new TreeNode();
                node.Text = dt.Rows[i]["MID"].ToString() + " " +  dt.Rows[i]["MNAME"].ToString();
                node.Value = dt.Rows[i]["MID"].ToString();
                node.ToolTip = strID;
                node.ShowCheckBox = true;
                e.Node.ChildNodes.Add(node);
            }
        }

        protected void chkListType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindTree(chkListType,tree);
            GetTestItemsValues(chkListType);
            if (chkListType.Items[0].Selected == false)
            {
                trWJ.Visible = false;
                
        }
            else
                trWJ.Visible = true;


            if (chkListType.Items[1].Selected == false)
            {
                trDPY.Visible = false;
                
            }
            else
                trDPY.Visible = true;
                
        }

        private void GetTestItemsValues(CheckBoxList chkListType)
        {
            lblWGValue.Text = "";
            lblDPValue.Text = "";
            if (chkListType.Items[0].Selected)
            {//外观
                lblWGValue.Text = "-";
            }

            if (chkListType.Items[1].Selected)
            {//底盘
                lblDPValue.Text = "-";
            }
        }

        protected void tree_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            //if (e.Node.ChildNodes.Count > 0)
            //{
            //    return;
            //}

            //string strWG = lblWGValue.Text.Trim();
            //string strDP = lblDPValue.Text.Trim();

            //if (!string.IsNullOrEmpty(e.Node.ToolTip) && Convert.ToInt32(e.Node.ToolTip) < 5)
            //{
            //    if (e.Node.Checked)
            //    {
            //        if (strWG.Contains("-"))
            //        {
            //            strWG.Replace("-", "");
            //        }
            //        strWG += e.Node.Value + "-1,";
            //    }
            //    else
            //    {
            //        if (strWG.Contains(e.Node.Value + "-1,"))
            //        {
            //            strWG.Replace(e.Node.Value + "-1,", "");
            //        }
            //    }
            //}

            //lblWGValue.Text = strWG;
        }

        protected void tree_SelectedNodeChanged(object sender, EventArgs e)
            {
            TreeNode node = sender as TreeNode;
            }

        protected void tree_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            TreeNode node = sender as TreeNode;
        }
        
        protected void ButtonOK_Click(object sender, EventArgs e)
          {

              this.Treeselected();

          }
        //遍历树方法
        private string GetReslut(TreeNode node)
                {
            string strRet = "";
            if (node.ChildNodes.Count > 0 && node.ChildNodes[0].ShowCheckBox == true)
                    {
                for (int i = 0; i < node.ChildNodes.Count; i++)
                {
                    if(node.ChildNodes[i].Checked)
                    {
                        strRet += node.ChildNodes[i].Value + "-1,";
                    }
                }
            }

            return strRet;
        }
        //保存
        protected void ButtonBC_Click(object sender, EventArgs e)
        {

            this.Treeselected();
            RESULT_CHASISS_MANUAL model = new RESULT_CHASISS_MANUAL();
            RESULT_CHASISS_MANUAL_BLL bll = new RESULT_CHASISS_MANUAL_BLL();
            model.JCLSH = jclsh.Value;
            model.RGJYBHGX = lblWGValue.Text;
            model.WGJCCZY = drpWJY.Text;
            model.DGJYBHGX = lblDPValue.Text;
            model.DPBJCZY = drpDPY.Text;
            bll.Insert(model);
            gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "保存成功！");
        }

        //绑定下拉列表
        private void binddrplist()
        {
            ButtonOK.Visible = false;
            EMPLOYEE_USER_BLL userbll = new EMPLOYEE_USER_BLL();
            var userinfoycy = userbll.GetModelList("LICENSE_NAME like'%02%'");
            for (int i = 0; i < userinfoycy.Length; i++)
            {
                drpYCY.Items.Add(userinfoycy[i].EMPLOYEE_NAME);
            }
            var userinfowjy = userbll.GetModelList("LICENSE_NAME like'%03%'");
            for (int i = 0; i < userinfowjy.Length; i++)
            {
                drpWJY.Items.Add(userinfowjy[i].EMPLOYEE_NAME);
            }
            var userinfodpy = userbll.GetModelList("LICENSE_NAME like'%04%'");
            for (int i = 0; i < userinfodpy.Length; i++)
        {
                drpDPY.Items.Add(userinfodpy[i].EMPLOYEE_NAME);
            }

        }

        protected void btnBrake_Click(object sender, EventArgs e)
        {
            string strHPHM = txtHPHM.Text.Trim();
            string str = "踩刹车";
            string strLineNum = txtJCXH.Text.Trim();
            SendData(strHPHM + "@" + str,Convert.ToInt32(strLineNum));
        }

        protected void ButtonSSC_Click(object sender, EventArgs e)
        {
            string strHPHM = txtHPHM.Text.Trim();
            string str = "松刹车";
            string strLineNum = txtJCXH.Text.Trim();
            SendData(strHPHM + "@" + str, Convert.ToInt32(strLineNum));
        }

        protected void ButtonZFX_Click(object sender, EventArgs e)
        {
            string strHPHM = txtHPHM.Text.Trim();
            string str = "左打方向盘";
            string strLineNum = txtJCXH.Text.Trim();
            SendData(strHPHM + "@" + str, Convert.ToInt32(strLineNum));
        }

        protected void ButtonYFX_Click(object sender, EventArgs e)
        {
            string strHPHM = txtHPHM.Text.Trim();
            string str = "右打方向盘";
            string strLineNum = txtJCXH.Text.Trim();
            SendData(strHPHM + "@" + str, Convert.ToInt32(strLineNum));
        }

        protected void ButtonHZFX_Click(object sender, EventArgs e)
        {
            string strHPHM = txtHPHM.Text.Trim();
            string str = "回正方向盘";
            string strLineNum = txtJCXH.Text.Trim();
            SendData(strHPHM + "@" + str, Convert.ToInt32(strLineNum));
        }

        protected void ButtonQJ_Click(object sender, EventArgs e)
        {
            string strHPHM = txtHPHM.Text.Trim();
            string str = "前进";
            string strLineNum = txtJCXH.Text.Trim();
            SendData(strHPHM + "@" + str, Convert.ToInt32(strLineNum));
        }

        protected void ButtonDC_Click(object sender, EventArgs e)
        {
            string strHPHM = txtHPHM.Text.Trim();
            string str = "倒车";
            string strLineNum = txtJCXH.Text.Trim();
            SendData(strHPHM + "@" + str, Convert.ToInt32(strLineNum));
        }

        protected void ButtonTC_Click(object sender, EventArgs e)
        {
            string strHPHM = txtHPHM.Text.Trim();
            string str = "停车";
            string strLineNum = txtJCXH.Text.Trim();
            SendData(strHPHM + "@" + str, Convert.ToInt32(strLineNum));
        } 
        private bool SendData(string strContent , int lineNum)
        {
            string strIPAddress = Config.AppConfig.g_UDPIPAddress;
            int Port = Convert.ToInt32(Config.AppConfig.g_UDPPort);
            UDPHelper udp = new UDPHelper(strIPAddress, Port);
            return udp.SendDataToLED(strContent,lineNum);
        }
        //遍历树
        private void Treeselected()
        {
            string strRetwg = "";
            string strRetdp = "";

            if (chkListType.Items[0].Selected == true && chkListType.Items[1].Selected == false)
            {
                for (int i = 0; i < tree.Nodes[0].ChildNodes.Count; i++)
                { strRetwg += GetReslut(tree.Nodes[0].ChildNodes[i]); }
                if (strRetwg == "")
                    lblWGValue.Text = "-";
                else
                    lblWGValue.Text = strRetwg;

            }
            if (chkListType.Items[0].Selected == false && chkListType.Items[1].Selected == true)
            {
                for (int i = 0; i < tree.Nodes[0].ChildNodes.Count; i++)
                { strRetdp += GetReslut(tree.Nodes[0].ChildNodes[i]); }
                if (strRetdp == "")
                    lblDPValue.Text = "-";
                else
                    lblDPValue.Text = strRetdp;


            }

            if (chkListType.Items[0].Selected == true && chkListType.Items[1].Selected == true)
            {
                for (int i = 0; i < tree.Nodes[0].ChildNodes.Count - 2; i++)
                { strRetwg += GetReslut(tree.Nodes[0].ChildNodes[i]); }
                if (strRetwg == "")
                    lblWGValue.Text = "-";
                else
                    lblWGValue.Text = strRetwg;
                for (int i = 5; i < tree.Nodes[0].ChildNodes.Count && i > 4; i++)
                { strRetdp += GetReslut(tree.Nodes[0].ChildNodes[i]); }
                if (strRetdp == "")
                    lblDPValue.Text = "-";
                else
                    lblDPValue.Text = strRetdp;


            }
        }
        //提车上线
        protected void btn_Click(object sender, EventArgs e)
        {
            string str = txtJCXH.Text;
            
            if (str==null || str.Length==0)
            {
                gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "请输入正确的检测线号！");
                return;
            }
            foreach (char c in str)
            {
                if (!Char.IsNumber(c))
                {
                    gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "请输入正确的检测线号！");
                }
                else
                { 
            VEHICLE_DISPATCH model = new VEHICLE_DISPATCH();
            VEHICLE_DISPATCH_BLL bll = new VEHICLE_DISPATCH_BLL();
            model.YCY = drpYCY.Text;
            model.WGJYY = drpWJY.Text;
            model.DPJYY = drpDPY.Text;
            model.JCZT_STATUS = 1;
            model.JCXH = int.Parse(txtJCXH.Text);
            bll.Update(model, jclsh.Value);
            gfLibrary.gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "提车上线成功！");
                
                }
        
            }
            


        }


        private string Getjclsh(string hphm, string hpzldh)
        {
            VEHICLE_DISPATCH_BLL result = new VEHICLE_DISPATCH_BLL();
            var resultbll = result.GetModelList("HPHM='" + hphm + "'and HPZLDH='" + hpzldh + "'order by ID desc ");
            if(resultbll.Length==0)
            { return ""; }
            else
            {
            string jclsh=resultbll[0].JCLSH;
            return jclsh;
            
            }
            
        
        
        }

    }
}